package com.css.zfzx.sjcj.common;

import lombok.Data;
import lombok.ToString;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;

/**
 * 接口返回结果数据公共接口
 *
 * @author zyl
 */
@Data
@ToString
public class RestResultData<T> implements Serializable {
    private static final long serialVersionUID = -7241863248931788742L;

    // 代码
    @NotBlank(message = "接口返回代码不能为空")
    private String code;
    // 操作返回信息
    private String message;
    // 操作返回数据
    private T data;
    private boolean result;

    /**
     * 操作成功
     */
    private static final String SUCCESS_CODE = "200";
    private static final String SUCCESS_MSG = "操作成功";
    /**
     * 操作失败
     */
    private static final String FAILURE_CODE = "500";
    private static final String FAILURE_MSG = "操作失败";
    /**
     * 接口调用成功,无对象返回
     *
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> ok() {
        return RestResultData.ok(SUCCESS_MSG, null);
    }

    /**
     * 接口调用成功,有对象返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> ok(T data) {
        return RestResultData.ok(SUCCESS_MSG, data);
    }

    /**
     * 接口调用成功,有描述信息
     * @param message
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> ok(String message) {
        RestResultData restResultData = new RestResultData();
        restResultData.setMessage(message);
        restResultData.setCode(SUCCESS_CODE);
        restResultData.setResult(true);
        return restResultData;
        //return RestResultData.ok(message, null);
    }

    /**
     * 接口调用成功,有对象返回,有描述信息
     *
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> ok(String message, T data) {
        RestResultData restResultData = new RestResultData();
        restResultData.setMessage(message);
        restResultData.setCode(SUCCESS_CODE);
        restResultData.setData(data);
        restResultData.setResult(true);
        return restResultData;
    }


    /**
     * 接口调用失败,无对象返回,无描述信息-500状态码
     *
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> fail() {
        return RestResultData.fail(FAILURE_MSG, FAILURE_CODE, null);
    }


    /**
     * 接口调用失败,有对象返回,500
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> fail(T data) {
        return RestResultData.fail(FAILURE_MSG, FAILURE_CODE, data);
    }

    /**
     * 失败,有描述信息
     *
     * @param message 失败描述
     * @param code    失败编码
     * @return
     */
    public static <T> RestResultData<T> fail(String message, String code) {
        return RestResultData.fail(message, code, null);
    }

    /**
     * 失败,有失败编码
     *
     * @param code
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> fail(String code) {
        return RestResultData.fail(FAILURE_MSG, code, null);
    }

    /**
     * 接口调用失败,有对象返回,有描述信息
     *
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResultData<T> fail(String message, String code, T data) {
        RestResultData restResultData = new RestResultData();
        restResultData.setMessage(message);
        restResultData.setCode(code);
        restResultData.setData(data);
        restResultData.setResult(false);
        return restResultData;
    }


    /**
     * 是否成功
     *
     * @param restResultData
     * @return
     */
    public static boolean isSuccess(@SuppressWarnings("rawtypes") RestResultData restResultData) {
        if (restResultData == null) {
            return false;
        }
        return SUCCESS_CODE == restResultData.getCode();
    }
}
